package com.blueocean_health.care.management.mapper;

import com.blueocean_health.care.management.domain.base.NurseGroupRelation;
import com.blueocean_health.care.management.domain.checkWorkVo.WorkListVo;
import com.blueocean_health.care.management.util.MyMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface NurseGroupRelationMapper extends MyMapper<NurseGroupRelation> {

    @Select("SELECT a.*FROM nurse_group_relation a ,nurse_group b " +
            "WHERE VERSION = " +
            "(SELECT MAX(VERSION) FROM nurse_group_relation WHERE a.group_id = `group_id`) " +
            "AND a.`group_id` = b.`group_id` " +
            "AND b.`deflag` = 0 " +
            "AND b.`hospital_id` =#{hospitalId}")
    public List<NurseGroupRelation> getAllMaxInfoByHospitalId(@Param("hospitalId") String hospitalId);

    @Select("SELECT a.*FROM nurse_group_relation a ,nurse_group b " +
            "WHERE VERSION = " +
            "(SELECT MAX(VERSION) FROM nurse_group_relation WHERE a.group_id = `group_id`) " +
            "AND a.`group_id` = b.`group_id` " +
            "AND b.`deflag` = 0 ")
    public List<NurseGroupRelation> getAllMaxInfo();

    @Select("SELECT a.*FROM nurse_group_relation a ,nurse_group b " +
            "WHERE VERSION = " +
            "(SELECT MAX(VERSION) FROM nurse_group_relation WHERE a.group_id = `group_id`) " +
            "AND a.`group_id` = b.`group_id` " +
            "AND b.`deflag` = 0 " +
            "AND b.`hospital_id` =#{hospitalId} AND a.group_id !=#{groupId}")
    public List<NurseGroupRelation> getAllMaxInfoByHospitalIdAndGroupId(@Param("hospitalId") String hospitalId,@Param("groupId") Integer groupId);


    @Update("UPDATE nurse_group_relation SET deflag = 1 WHERE nurse_id =#{nurseId}")
    public int updateNurseGroupRelationDeflag(@Param("nurseId") String nurseId);

    @Update("UPDATE nurse_group_relation SET deflag = 1 WHERE group_id =#{groupId}")
    public int updateNurseGroupRelationDeflagByGroupId(@Param("groupId") Integer groupId);

    @Select(" SELECT *FROM nurse_group_relation n WHERE n.`deflag` = 0 AND  n.`nurse_id` =#{nurseId} ORDER BY n.`version` DESC LIMIT 1 ")
    public NurseGroupRelation queryInfoByWorkId(@Param("nurseId") String nurseId);

    @Select("SELECT a.`nurse_id` as workId,a.`group_id`,c.`worker_name` as workName FROM nurse_group_relation a ,nurse_group b ,nurse_worker_info c  " +
            "WHERE VERSION =  " +
            "(SELECT MAX(VERSION) FROM nurse_group_relation WHERE a.group_id = `group_id`) " +
            "AND a.`group_id` = b.`group_id`  " +
            "AND a.`nurse_id` = c.`worker_id`  " +
            "AND a.`deflag` = 0 " +
            "AND a.`group_id` =#{groupId}")
    public List<WorkListVo> queryWorkerListForCheckWork(@Param("groupId") Integer groupId);


}